<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  

  
  <title>Spring-secrity-role-basedLogin | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Spring Security基于角色登录实例 程序可根据自己分配的角色在登录时将用户重定向到不同的URL。 最基本的，我们要做的是创建一个自定义的成功处理程序来负责根据登录用户的角色重定向到相应的URL。Spring Security中已经提供了 SimpleUrlAuthenticationSuccessHandler，它包含了成功处理程序的逻辑。我们只是用自己的重定向逻辑扩展它来实现我们的目">
<meta name="keywords" content="Spring-secrity">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring-secrity-role-basedLogin">
<meta property="og:url" content="http://yoursite.com/2018/10/12/2017-12-04-Spring-secrity-role-basedLogin/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="Spring Security基于角色登录实例 程序可根据自己分配的角色在登录时将用户重定向到不同的URL。 最基本的，我们要做的是创建一个自定义的成功处理程序来负责根据登录用户的角色重定向到相应的URL。Spring Security中已经提供了 SimpleUrlAuthenticationSuccessHandler，它包含了成功处理程序的逻辑。我们只是用自己的重定向逻辑扩展它来实现我们的目">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121158_1_606.png">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121214_1_a6.png">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121241_1_933.png">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121258_1_3q.png">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121317_1_g8.png">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121349_1_4f.png">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121415_1_161.png">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121431_1_t3.png">
<meta property="og:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121448_1_252.png">
<meta property="og:updated_time" content="2018-11-01T14:24:04.913Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Spring-secrity-role-basedLogin">
<meta name="twitter:description" content="Spring Security基于角色登录实例 程序可根据自己分配的角色在登录时将用户重定向到不同的URL。 最基本的，我们要做的是创建一个自定义的成功处理程序来负责根据登录用户的角色重定向到相应的URL。Spring Security中已经提供了 SimpleUrlAuthenticationSuccessHandler，它包含了成功处理程序的逻辑。我们只是用自己的重定向逻辑扩展它来实现我们的目">
<meta name="twitter:image" content="http://www.yiibai.com/uploads/tutorial/20160820/160r0121158_1_606.png">
  
    <link rel="alternate" href="/org/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/org/css/style.css">
</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/org/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/org/">Home</a>
        
          <a class="main-nav-link" href="/org/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/org/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-2017-12-04-Spring-secrity-role-basedLogin" class="article article-type-post" itemscope="" itemprop="blogPost">
  <div class="article-meta">
    <a href="/org/2018/10/12/2017-12-04-Spring-secrity-role-basedLogin/" class="article-date">
  <time datetime="2018-10-12T09:03:30.000Z" itemprop="datePublished">2018-10-12</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Spring-secrity-role-basedLogin
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>Spring Security基于角色登录实例</p>
<p>程序可根据自己分配的角色在登录时将用户重定向到不同的URL。</p>
<p>最基本的，我们要做的是创建一个自定义的成功处理程序来负责根据登录用户的角色重定向到相应的URL。Spring Security中已经提供了 SimpleUrlAuthenticationSuccessHandler，它包含了成功处理程序的逻辑。我们只是用自己的重定向逻辑扩展它来实现我们的目标。</p>
<a id="more"></a>
<p>当这个处理程序成功，我们将通过注册 formLogin() 或 loginPage()。完整的例子如下所示</p>
<p>用到的技术如下：</p>
<ul>
<li>Spring 3.2.RELEASE</li>
<li>Spring Security 4.0.1.RELEASE</li>
<li>Maven 3</li>
<li>JDK 1.8</li>
<li>Tomcat 7</li>
<li>Eclipse JUNO Service Release 2</li>
</ul>
<p>让我们现在就开始，建议您按照以下步骤一起来实践。</p>
<h4 id="步骤1-工程目录结构"><a href="#步骤1-工程目录结构" class="headerlink" title="步骤1: 工程目录结构"></a>步骤1: 工程目录结构</h4><p>现在，让我们解释在上面添加的结构每个细节提及的内容。</p>
<h4 id="第2步：更新pom-xml，包括所需依懒"><a href="#第2步：更新pom-xml，包括所需依懒" class="headerlink" title="第2步：更新pom.xml，包括所需依懒"></a>第2步：更新pom.xml，包括所需依懒</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line">&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;</span><br><span class="line">  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;</span><br><span class="line">  &lt;groupId&gt;com.anlu.secrity&lt;/groupId&gt;</span><br><span class="line">  &lt;artifactId&gt;Spring-secrity-taglibs&lt;/artifactId&gt;</span><br><span class="line">  &lt;packaging&gt;war&lt;/packaging&gt;</span><br><span class="line">  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;</span><br><span class="line">  &lt;name&gt;Spring-secrity-taglibs Maven Webapp&lt;/name&gt;</span><br><span class="line">  &lt;url&gt;http://maven.apache.org&lt;/url&gt;</span><br><span class="line">  &lt;properties&gt;</span><br><span class="line">    &lt;!--&lt;spring.security.version&gt;4.0.1.RELEASE&lt;/spring.security.version&gt;--&gt;</span><br><span class="line">    &lt;spring.security.version&gt;3.2.3.RELEASE&lt;/spring.security.version&gt;</span><br><span class="line">  &lt;/properties&gt;</span><br><span class="line">  &lt;dependencies&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;3.8.1&lt;/version&gt;</span><br><span class="line">      &lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">    &lt;!--日志包--&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;junit&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;junit&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;3.8.1&lt;/version&gt;</span><br><span class="line">      &lt;scope&gt;test&lt;/scope&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.slf4j&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;1.7.21&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--j2ee相关包 servlet、jsp、jstl--&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;3.1.0&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;2.2&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;jstl&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;1.2&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--mysql驱动包--&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;mysql&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;5.1.35&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--spring相关包--&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-web&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;4.3.1.RELEASE&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;4.3.1.RELEASE&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!--其他需要的包--&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;3.4&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;commons-fileupload&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;1.3.1&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">    &lt;!-- Spring Security --&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;$&#123;spring.security.version&#125;&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;$&#123;spring.security.version&#125;&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;spring-security-taglibs&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;$&#123;spring.security.version&#125;&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line">  &lt;/dependencies&gt;</span><br><span class="line">  &lt;build&gt;</span><br><span class="line">    &lt;finalName&gt;Spring-secrity-role-basedLogin&lt;/finalName&gt;</span><br><span class="line">  &lt;/build&gt;</span><br><span class="line">&lt;/project&gt;</span><br></pre></td></tr></table></figure>
<h4 id="步骤3-添加Spring-Security配置类"><a href="#步骤3-添加Spring-Security配置类" class="headerlink" title="步骤3: 添加Spring Security配置类"></a>步骤3: 添加Spring Security配置类</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;beans:beans xmlns=&quot;http://www.springframework.org/schema/security&quot;</span><br><span class="line">             xmlns:beans=&quot;http://www.springframework.org/schema/beans&quot;</span><br><span class="line">             xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">             xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans</span><br><span class="line">	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</span><br><span class="line">	http://www.springframework.org/schema/security</span><br><span class="line">	http://www.springframework.org/schema/security/spring-security-3.2.xsd&quot;&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &lt;http auto-config=&quot;true&quot;&gt;</span><br><span class="line">        &lt;intercept-url pattern=&quot;/&quot; access=&quot;ROLE_USER&quot;/&gt;</span><br><span class="line">        &lt;intercept-url pattern=&quot;/home&quot; access=&quot;ROLE_USER&quot;/&gt;</span><br><span class="line">        &lt;intercept-url pattern=&quot;/admin**&quot; access=&quot;ROLE_ADMIN&quot;/&gt;</span><br><span class="line">        &lt;intercept-url pattern=&quot;/dba**&quot; access=&quot;ROLE_ADMIN and ROLE_DBA&quot;/&gt;</span><br><span class="line">        &lt;form-login login-page=&quot;/login&quot;</span><br><span class="line">                    username-parameter=&quot;username&quot;</span><br><span class="line">                    password-parameter=&quot;password&quot;</span><br><span class="line">                    authentication-success-handler-ref=&quot;customSuccessHandler&quot;</span><br><span class="line">                    authentication-failure-url=&quot;/Access_Denied&quot;/&gt;</span><br><span class="line">        &lt;csrf/&gt;</span><br><span class="line">    &lt;/http&gt;</span><br><span class="line"></span><br><span class="line">    &lt;authentication-manager&gt;</span><br><span class="line">        &lt;authentication-provider&gt;</span><br><span class="line">            &lt;user-service&gt;</span><br><span class="line">                &lt;user name=&quot;anlu&quot; password=&quot;123456&quot; authorities=&quot;ROLE_USER&quot;/&gt;</span><br><span class="line">                &lt;user name=&quot;admin&quot; password=&quot;123456&quot; authorities=&quot;ROLE_ADMIN&quot;/&gt;</span><br><span class="line">                &lt;user name=&quot;dba&quot; password=&quot;123456&quot; authorities=&quot;ROLE_ADMIN,ROLE_DBA&quot;/&gt;</span><br><span class="line">            &lt;/user-service&gt;</span><br><span class="line">        &lt;/authentication-provider&gt;</span><br><span class="line">    &lt;/authentication-manager&gt;</span><br><span class="line"></span><br><span class="line">    &lt;beans:bean id=&quot;customSuccessHandler&quot; class=&quot;com.anlu.secrity.config.CustomSuccessHandler&quot;/&gt;</span><br><span class="line"></span><br><span class="line">&lt;/beans:beans&gt;</span><br></pre></td></tr></table></figure>
<p>下面是上面提及成功处理的类，如下图的所示 -</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line">package com.anlu.secrity.config;</span><br><span class="line"></span><br><span class="line">import org.springframework.security.core.Authentication;</span><br><span class="line">import org.springframework.security.core.GrantedAuthority;</span><br><span class="line">import org.springframework.security.web.DefaultRedirectStrategy;</span><br><span class="line">import org.springframework.security.web.RedirectStrategy;</span><br><span class="line">import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;</span><br><span class="line">import org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line">import javax.servlet.ServletException;</span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line">import java.io.IOException;</span><br><span class="line">import java.util.ArrayList;</span><br><span class="line">import java.util.Collection;</span><br><span class="line">import java.util.List;</span><br><span class="line"></span><br><span class="line">@Component</span><br><span class="line">public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler &#123;</span><br><span class="line"></span><br><span class="line">    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException &#123;</span><br><span class="line">        String targetUrl = determineTargetUrl(authentication);</span><br><span class="line">        if (response.isCommitted()) &#123;</span><br><span class="line">            System.out.println(&quot;Can&apos;t redirect&quot;);</span><br><span class="line">            return;</span><br><span class="line">        &#125;</span><br><span class="line">        redirectStrategy.sendRedirect(request, response, targetUrl);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /*</span><br><span class="line">	 * This method extracts the roles of currently logged-in user and returns</span><br><span class="line">	 * appropriate URL according to his/her role.</span><br><span class="line">	 */</span><br><span class="line">    protected String determineTargetUrl(Authentication authentication) &#123;</span><br><span class="line">        String url = &quot;&quot;;</span><br><span class="line"></span><br><span class="line">        Collection&lt;? extends GrantedAuthority&gt; authorities = authentication.getAuthorities();</span><br><span class="line"></span><br><span class="line">        List&lt;String&gt; roles = new ArrayList&lt;String&gt;();</span><br><span class="line"></span><br><span class="line">        for (GrantedAuthority a : authorities) &#123;</span><br><span class="line">            roles.add(a.getAuthority());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        if (isDba(roles)) &#123;</span><br><span class="line">            url = &quot;/db&quot;;</span><br><span class="line">        &#125; else if (isAdmin(roles)) &#123;</span><br><span class="line">            url = &quot;/admin&quot;;</span><br><span class="line">        &#125; else if (isUser(roles)) &#123;</span><br><span class="line">            url = &quot;/home&quot;;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            url = &quot;/accessDenied&quot;;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        return url;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private boolean isUser(List&lt;String&gt; roles) &#123;</span><br><span class="line">        if (roles.contains(&quot;ROLE_USER&quot;)) &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125;</span><br><span class="line">        return false;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private boolean isAdmin(List&lt;String&gt; roles) &#123;</span><br><span class="line">        if (roles.contains(&quot;ROLE_ADMIN&quot;)) &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125;</span><br><span class="line">        return false;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private boolean isDba(List&lt;String&gt; roles) &#123;</span><br><span class="line">        if (roles.contains(&quot;ROLE_DBA&quot;)) &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125;</span><br><span class="line">        return false;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public void setRedirectStrategy(RedirectStrategy redirectStrategy) &#123;</span><br><span class="line">        this.redirectStrategy = redirectStrategy;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected RedirectStrategy getRedirectStrategy() &#123;</span><br><span class="line">        return redirectStrategy;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意：看看如何扩展 SimpleUrlAuthenticationSuccessHandler类和overridinghandle()方法，这个方法简单地使用配置RedirectStrategy[默认在这种情况下]调用重定向，由用户定义determineTargetUrl方法返回URL。此方法提取当前认证对象用户记录的角色，然后构造基于角色有相应的URL。最后RedirectStrategy，这是负责 Spring Security 框架内的所有重定向，请求重定向到指定的URL。</p>
</blockquote>
<blockquote>
<p>以下步骤：其余的都是和之前教程文章中的内容相同了。</p>
</blockquote>
<h4 id="web-xml中配置如下"><a href="#web-xml中配置如下" class="headerlink" title="web.xml中配置如下:"></a>web.xml中配置如下:</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span><br><span class="line">&lt;web-app xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;</span><br><span class="line">         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span><br><span class="line">         xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee</span><br><span class="line">          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;</span><br><span class="line">         version=&quot;3.0&quot;&gt;</span><br><span class="line"></span><br><span class="line">  &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;</span><br><span class="line"></span><br><span class="line">  &lt;!-- Spring MVC --&gt;</span><br><span class="line">  &lt;servlet&gt;</span><br><span class="line">    &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;</span><br><span class="line">    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet</span><br><span class="line">    &lt;/servlet-class&gt;</span><br><span class="line">    &lt;init-param&gt;</span><br><span class="line">      &lt;!--Sources标注的文件夹下需要新建一个spring文件夹--&gt;</span><br><span class="line">      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;</span><br><span class="line">      &lt;param-value&gt;classpath:spring/spring-mvc.xml&lt;/param-value&gt;</span><br><span class="line">    &lt;/init-param&gt;</span><br><span class="line">    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;</span><br><span class="line">  &lt;/servlet&gt;</span><br><span class="line">  &lt;servlet-mapping&gt;</span><br><span class="line">    &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;</span><br><span class="line">    &lt;url-pattern&gt;/&lt;/url-pattern&gt;</span><br><span class="line">  &lt;/servlet-mapping&gt;</span><br><span class="line"></span><br><span class="line">  &lt;listener&gt;</span><br><span class="line">    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener</span><br><span class="line">    &lt;/listener-class&gt;</span><br><span class="line">  &lt;/listener&gt;</span><br><span class="line"></span><br><span class="line">  &lt;!-- Loads Spring Security config file --&gt;</span><br><span class="line">  &lt;context-param&gt;</span><br><span class="line">    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;</span><br><span class="line">    &lt;param-value&gt;</span><br><span class="line">      classpath:/spring/spring-secrity.xml</span><br><span class="line">    &lt;/param-value&gt;</span><br><span class="line">  &lt;/context-param&gt;</span><br><span class="line"></span><br><span class="line">  &lt;!-- Spring Security --&gt;</span><br><span class="line">  &lt;filter&gt;</span><br><span class="line">    &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;</span><br><span class="line">    &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy</span><br><span class="line">    &lt;/filter-class&gt;</span><br><span class="line">  &lt;/filter&gt;</span><br><span class="line"></span><br><span class="line">  &lt;filter-mapping&gt;</span><br><span class="line">    &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;</span><br><span class="line">    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;</span><br><span class="line">  &lt;/filter-mapping&gt;</span><br><span class="line">&lt;/web-app&gt;</span><br></pre></td></tr></table></figure>
<h4 id="第5步-添加控制器"><a href="#第5步-添加控制器" class="headerlink" title="第5步: 添加控制器"></a>第5步: 添加控制器</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">package com.anlu.secrity.controller;</span><br><span class="line"></span><br><span class="line">import org.springframework.security.core.Authentication;</span><br><span class="line">import org.springframework.security.core.context.SecurityContextHolder;</span><br><span class="line">import org.springframework.security.core.userdetails.UserDetails;</span><br><span class="line">import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;</span><br><span class="line">import org.springframework.stereotype.Controller;</span><br><span class="line">import org.springframework.ui.ModelMap;</span><br><span class="line">import org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line">import org.springframework.web.bind.annotation.RequestMethod;</span><br><span class="line">import org.springframework.web.bind.annotation.RequestParam;</span><br><span class="line">import org.springframework.web.servlet.ModelAndView;</span><br><span class="line"></span><br><span class="line">import javax.servlet.http.HttpServletRequest;</span><br><span class="line">import javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line">@Controller</span><br><span class="line">public class HelloController &#123;</span><br><span class="line">    @RequestMapping(value = &#123; &quot;/&quot;, &quot;/home&quot; &#125;, method = RequestMethod.GET)</span><br><span class="line">    public String homePage(ModelMap model) &#123;</span><br><span class="line">        model.addAttribute(&quot;user&quot;, getPrincipal());</span><br><span class="line">        return &quot;welcome&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @RequestMapping(value = &quot;/admin&quot;, method = RequestMethod.GET)</span><br><span class="line">    public String adminPage(ModelMap model) &#123;</span><br><span class="line">        model.addAttribute(&quot;user&quot;, getPrincipal());</span><br><span class="line">        return &quot;admin&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @RequestMapping(value = &quot;/db&quot;, method = RequestMethod.GET)</span><br><span class="line">    public String dbaPage(ModelMap model) &#123;</span><br><span class="line">        model.addAttribute(&quot;user&quot;, getPrincipal());</span><br><span class="line">        return &quot;dba&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @RequestMapping(value = &quot;/Access_Denied&quot;, method = RequestMethod.GET)</span><br><span class="line">    public String accessDeniedPage(ModelMap model) &#123;</span><br><span class="line">        model.addAttribute(&quot;user&quot;, getPrincipal());</span><br><span class="line">        return &quot;denied&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @RequestMapping(value = &quot;/login&quot;, method = RequestMethod.GET)</span><br><span class="line">    public ModelAndView login(@RequestParam(value = &quot;error&quot;, required = false) String error,</span><br><span class="line">                              @RequestParam(value = &quot;logout&quot;, required = false) String logout) &#123;</span><br><span class="line">        ModelAndView model = new ModelAndView();</span><br><span class="line">        if (error != null) &#123;</span><br><span class="line">            model.addObject(&quot;error&quot;, &quot;Invalid username and password!&quot;);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        if (logout != null) &#123;</span><br><span class="line">            model.addObject(&quot;msg&quot;, &quot;You&apos;ve been logged out successfully.&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        model.setViewName(&quot;login&quot;);</span><br><span class="line"></span><br><span class="line">        return model;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @RequestMapping(value=&quot;/logout&quot;, method = RequestMethod.GET)</span><br><span class="line">    public String logoutPage (HttpServletRequest request, HttpServletResponse response) &#123;</span><br><span class="line">        Authentication auth = SecurityContextHolder.getContext().getAuthentication();</span><br><span class="line">        if (auth != null)&#123;</span><br><span class="line">            new SecurityContextLogoutHandler().logout(request, response, auth);</span><br><span class="line">        &#125;</span><br><span class="line">        return &quot;redirect:/login?logout&quot;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private String getPrincipal()&#123;</span><br><span class="line">        String userName = null;</span><br><span class="line">        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();</span><br><span class="line"></span><br><span class="line">        if (principal instanceof UserDetails) &#123;</span><br><span class="line">            userName = ((UserDetails)principal).getUsername();</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            userName = principal.toString();</span><br><span class="line">        &#125;</span><br><span class="line">        return userName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="第6步-添加jsp页面"><a href="#第6步-添加jsp页面" class="headerlink" title="第6步: 添加jsp页面"></a>第6步: 添加jsp页面</h4><p>login.jsp</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line">&lt;%--</span><br><span class="line">  Created by IntelliJ IDEA.</span><br><span class="line">  User: Anlu</span><br><span class="line">  Date: 2017/12/2</span><br><span class="line">  Time: 14:56</span><br><span class="line">  To change this template use File | Settings | File Templates.</span><br><span class="line">--%&gt;</span><br><span class="line">&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;</span><br><span class="line">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;title&gt;Login Page&lt;/title&gt;</span><br><span class="line">    &lt;style&gt;</span><br><span class="line">        .error &#123;</span><br><span class="line">            padding: 15px;</span><br><span class="line">            margin-bottom: 20px;</span><br><span class="line">            border: 1px solid transparent;</span><br><span class="line">            border-radius: 4px;</span><br><span class="line">            color: #a94442;</span><br><span class="line">            background-color: #f2dede;</span><br><span class="line">            border-color: #ebccd1;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        .msg &#123;</span><br><span class="line">            padding: 15px;</span><br><span class="line">            margin-bottom: 20px;</span><br><span class="line">            border: 1px solid transparent;</span><br><span class="line">            border-radius: 4px;</span><br><span class="line">            color: #31708f;</span><br><span class="line">            background-color: #d9edf7;</span><br><span class="line">            border-color: #bce8f1;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        #login-box &#123;</span><br><span class="line">            width: 300px;</span><br><span class="line">            padding: 20px;</span><br><span class="line">            margin: 100px auto;</span><br><span class="line">            background: #fff;</span><br><span class="line">            -webkit-border-radius: 2px;</span><br><span class="line">            -moz-border-radius: 2px;</span><br><span class="line">            border: 1px solid #000;</span><br><span class="line">        &#125;</span><br><span class="line">    &lt;/style&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body onload=&apos;document.loginForm.username.focus();&apos;&gt;</span><br><span class="line"></span><br><span class="line">&lt;h1&gt;Spring Security Custom Login Form (XML)&lt;/h1&gt;</span><br><span class="line"></span><br><span class="line">&lt;div id=&quot;login-box&quot;&gt;</span><br><span class="line"></span><br><span class="line">    &lt;h2&gt;Login with Username and Password&lt;/h2&gt;</span><br><span class="line"></span><br><span class="line">    &lt;c:if test=&quot;$&#123;not empty error&#125;&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;error&quot;&gt;$&#123;error&#125;&lt;/div&gt;</span><br><span class="line">    &lt;/c:if&gt;</span><br><span class="line">    &lt;c:if test=&quot;$&#123;not empty msg&#125;&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;msg&quot;&gt;$&#123;msg&#125;&lt;/div&gt;</span><br><span class="line">    &lt;/c:if&gt;</span><br><span class="line"></span><br><span class="line">    &lt;form name=&apos;loginForm&apos;</span><br><span class="line">          action=&quot;&lt;c:url value=&apos;j_spring_security_check&apos; /&gt;&quot; method=&apos;POST&apos;&gt;</span><br><span class="line"></span><br><span class="line">        &lt;table&gt;</span><br><span class="line">            &lt;tr&gt;</span><br><span class="line">                &lt;td&gt;User:&lt;/td&gt;</span><br><span class="line">                &lt;td&gt;&lt;input type=&apos;text&apos; name=&apos;username&apos; value=&apos;&apos;&gt;&lt;/td&gt;</span><br><span class="line">            &lt;/tr&gt;</span><br><span class="line">            &lt;tr&gt;</span><br><span class="line">                &lt;td&gt;Password:&lt;/td&gt;</span><br><span class="line">                &lt;td&gt;&lt;input type=&apos;password&apos; name=&apos;password&apos; /&gt;&lt;/td&gt;</span><br><span class="line">            &lt;/tr&gt;</span><br><span class="line">            &lt;tr&gt;</span><br><span class="line">                &lt;td colspan=&apos;2&apos;&gt;&lt;input name=&quot;submit&quot; type=&quot;submit&quot;</span><br><span class="line">                                       value=&quot;submit&quot; /&gt;&lt;/td&gt;</span><br><span class="line">            &lt;/tr&gt;</span><br><span class="line">        &lt;/table&gt;</span><br><span class="line"></span><br><span class="line">        &lt;input type=&quot;hidden&quot; name=&quot;$&#123;_csrf.parameterName&#125;&quot;</span><br><span class="line">               value=&quot;$&#123;_csrf.token&#125;&quot; /&gt;</span><br><span class="line"></span><br><span class="line">    &lt;/form&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<p>welcome.jsp</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;%--</span><br><span class="line">  Created by IntelliJ IDEA.</span><br><span class="line">  User: Anlu</span><br><span class="line">  Date: 2017/12/4</span><br><span class="line">  Time: 16:33</span><br><span class="line">  To change this template use File | Settings | File Templates.</span><br><span class="line">--%&gt;</span><br><span class="line">&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;</span><br><span class="line">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&gt;</span><br><span class="line">    &lt;title&gt;Welcome page&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">Dear &lt;strong&gt;$&#123;user&#125;&lt;/strong&gt;, Welcome to Home Page.</span><br><span class="line">&lt;a href=&quot;&lt;c:url value=&quot;/logout&quot; /&gt;&quot;&gt;Logout&lt;/a&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<p>admin.jsp</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;%--</span><br><span class="line">  Created by IntelliJ IDEA.</span><br><span class="line">  User: Anlu</span><br><span class="line">  Date: 2017/12/4</span><br><span class="line">  Time: 16:33</span><br><span class="line">  To change this template use File | Settings | File Templates.</span><br><span class="line">--%&gt;</span><br><span class="line">&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;</span><br><span class="line">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;title&gt;Admin page&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">Dear &lt;strong&gt;$&#123;user&#125;&lt;/strong&gt;, Welcome to Admin Page.</span><br><span class="line">&lt;a href=&quot;&lt;c:url value=&quot;/logout&quot; /&gt;&quot;&gt;Logout&lt;/a&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<p>dba.jsp</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;%--</span><br><span class="line">  Created by IntelliJ IDEA.</span><br><span class="line">  User: Anlu</span><br><span class="line">  Date: 2017/12/4</span><br><span class="line">  Time: 16:34</span><br><span class="line">  To change this template use File | Settings | File Templates.</span><br><span class="line">--%&gt;</span><br><span class="line">&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;</span><br><span class="line">&lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;title&gt;DBA page&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">Dear &lt;strong&gt;$&#123;user&#125;&lt;/strong&gt;, Welcome to DBA Page.</span><br><span class="line">&lt;a href=&quot;&lt;c:url value=&quot;/logout&quot; /&gt;&quot;&gt;Logout&lt;/a&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<p>denied.jsp</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">&lt;%--</span><br><span class="line">  Created by IntelliJ IDEA.</span><br><span class="line">  User: Anlu</span><br><span class="line">  Date: 2017/12/4</span><br><span class="line">  Time: 14:51</span><br><span class="line">  To change this template use File | Settings | File Templates.</span><br><span class="line">--%&gt;</span><br><span class="line">&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;title&gt;Title&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;h1&gt;您的访问被拒绝了!&lt;/h1&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<h4 id="第7步：构建和部署应用程序"><a href="#第7步：构建和部署应用程序" class="headerlink" title="第7步：构建和部署应用程序"></a>第7步：构建和部署应用程序</h4><p>打开浏览器，然后访问：<a href="http://localhost:8080/SpringSecurityRoleBasedLogin" target="_blank" rel="noopener">http://localhost:8080/SpringSecurityRoleBasedLogin</a> ，如果没有任何错误，将提示登录 - </p>
<p><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121158_1_606.png" alt="img"></p>
<p>DBA提供的凭据(用户名和密码登录)，如下所示：</p>
<p><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121214_1_a6.png" alt="img"></p>
<p>提交，您将会被跳转到 /db 页面，作为登录的用户具有DBA角色(基于角色的登录)。如下图中所示 - </p>
<p><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121241_1_933.png" alt="img"></p>
<p>现在注销，并填写用户(yiibai)角色的凭据(使用用户名和密码)登录。</p>
<p><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121258_1_3q.png" alt="img"></p>
<p>首先提供一个错误的密码，然后点击登录 - </p>
<p><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121317_1_g8.png" alt="img"></p>
<p>提供正确的用户角色的凭据，您将被重定向到主页。</p>
<p><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121349_1_4f.png" alt="img"></p>
<p>现在尝试访问管理页面。您应该看到拒绝访问页面。</p>
<p><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121415_1_161.png" alt="img"></p>
<p>现在，注销并使用管理员凭据登录，您将会被重向到URL：/admin 。如下图中所示 - </p>
<p><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121431_1_t3.png" alt="img"></p>
<p>用户 admin 登录成功后的页面，如下所示 -<br><img src="http://www.yiibai.com/uploads/tutorial/20160820/160r0121448_1_252.png" alt="img"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/10/12/2017-12-04-Spring-secrity-role-basedLogin/" data-id="cjoztxu1n00031wijqimcm2n8" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/org/tags/Spring-secrity/">Spring-secrity</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/org/2018/10/12/2018-07-31-Hibernate的基本用法/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          hibernate的基本用法
        
      </div>
    </a>
  
  
    <a href="/org/2018/10/12/2018-01-01-使用jekyll+码云搭建博客/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">使用jekyll搭建博客</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/SVN/">SVN</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/Spring-secrity/">Spring-secrity</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/hibenrate/">hibenrate</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/jekyll/">jekyll</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/jenkins/">jenkins</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/redis/">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/shiro/">shiro</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spingMVC/">spingMVC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-cloud/">spring cloud</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-cloud/">spring-cloud</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/spring-secrity/">spring-secrity</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/springMVC/">springMVC</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/springboot/">springboot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/org/tags/生活/">生活</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/org/tags/SVN/" style="font-size: 12px;">SVN</a> <a href="/org/tags/Spring-secrity/" style="font-size: 10px;">Spring-secrity</a> <a href="/org/tags/hexo/" style="font-size: 10px;">hexo</a> <a href="/org/tags/hibenrate/" style="font-size: 10px;">hibenrate</a> <a href="/org/tags/jekyll/" style="font-size: 10px;">jekyll</a> <a href="/org/tags/jenkins/" style="font-size: 10px;">jenkins</a> <a href="/org/tags/redis/" style="font-size: 16px;">redis</a> <a href="/org/tags/shiro/" style="font-size: 18px;">shiro</a> <a href="/org/tags/spingMVC/" style="font-size: 10px;">spingMVC</a> <a href="/org/tags/spring-cloud/" style="font-size: 10px;">spring cloud</a> <a href="/org/tags/spring-cloud/" style="font-size: 10px;">spring-cloud</a> <a href="/org/tags/spring-secrity/" style="font-size: 12px;">spring-secrity</a> <a href="/org/tags/springMVC/" style="font-size: 14px;">springMVC</a> <a href="/org/tags/springboot/" style="font-size: 20px;">springboot</a> <a href="/org/tags/生活/" style="font-size: 10px;">生活</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/org/archives/2018/11/">November 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/org/archives/2018/10/">October 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/org/2018/11/27/redis07-zookeeper-kafka集群部署以及如何使用简单介绍/">redis07-zookeeper+kafka集群部署以及如何使用简单介绍</a>
          </li>
        
          <li>
            <a href="/org/2018/11/13/redis06-cluster实现高可用性/">redis06-cluster实现高可用性</a>
          </li>
        
          <li>
            <a href="/org/2018/11/12/redis05-在项目中搭建读写分-高可用-多master的redis-cluster集群/">redis05-在项目中搭建读写分+高可用+多master的redis cluster集群</a>
          </li>
        
          <li>
            <a href="/org/2018/11/05/在项目中用经典的三节点方式部署哨兵集群-笔记/">在项目中用经典的三节点方式部署哨兵集群-笔记</a>
          </li>
        
          <li>
            <a href="/org/2018/11/05/redis哨兵的多个核心底层原理-笔记/">redis哨兵的多个核心底层原理-笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/org/" class="mobile-nav-link">Home</a>
  
    <a href="/org/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//code.jquery.com/jquery-2.0.3.min.js"></script>


  <link rel="stylesheet" href="/org/fancybox/jquery.fancybox.css">
  <script src="/org/fancybox/jquery.fancybox.pack.js"></script>


<script src="/org/js/script.js"></script>



  </div>
</body>
</html>